<?php
namespace Amoysea\Controller;
use Think\Controller;
class FinanceController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
		C('erp_dbname',$g['db']);
        $db_name=$g['db'];
    }
	//采购单
    public function list_list(){
        $m=M('List', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$n=M('ListProduct', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$p=I('p')?I('p'):1;
		$listrows=I('listrows')?I('listrows'):15;
		$firstrow=($p-1)*$listrows;
		$param = I('param.','','trim');
		$order_no=$param['order_no'];
		$map=array();
		if(!empty($order_no)){
			$map['order_no'] = array('like','%'.$order_no.'%');
		}
		$start_time=$param['start_time'];
		$end_time=$param['end_time'];
		if($start_time!='') {
            $map['created_at']= array('gt',$start_time);
        }
        if($end_time!=''){
            $map['created_at'] = array('lt',$end_time);
        }
        if($start_time!='' && $end_time!=''){
            $map['created_at']= array(array('gt',$start_time),array('lt',$end_time));
        }
		// dump($map);
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$total_fee=0;
		foreach($list as $k=>$v){
			$p_list=$n->where(array('list_id'=>$v['id']))->select();
			$total_price=0;
			foreach($p_list as $ks=>$vs){
				$total_price+=$vs['price']*$vs['count'];
			}
			$list[$k]['total_price']=$total_price;
			$total_fee+=$total_price;
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		$this->assign("total_fee",$total_fee);
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	//销售单
    public function salesman(){
        $m=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$n=M('PushordergoodsSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$pe=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$c=M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$s=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$p=I('p')?I('p'):1;
		$listrows=I('listrows')?I('listrows'):15;
		$firstrow=($p-1)*$listrows;
		$param = I('param.','','trim');
		$order_no=$param['order_no'];
		$map=array();
		if(!empty($order_no)){
			$map['order_no'] = array('like','%'.$order_no.'%');
		}
		$start_time=$param['start_time'];
		$end_time=$param['end_time'];
		if($start_time!='') {
            $map['created_at']= array('gt',$start_time);
        }
        if($end_time!=''){
            $map['created_at'] = array('lt',$end_time);
        }
        if($start_time!='' && $end_time!=''){
            $map['created_at']= array(array('gt',$start_time),array('lt',$end_time));
        }
		// dump($map);
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$total_fee=0;
		foreach($list as $k=>$v){
			$p_list=$n->where(array('order_id'=>$v['id']))->select();
			$total_price=0;
			foreach($p_list as $ks=>$vs){
				$total_price+=$vs['goods_price']*$vs['goods_nums'];
			}
			$list[$k]['total_price']=$total_price;
			$total_fee+=$total_price;
			$department=$s->where(array('no'=>$v['section_no']))->find();
			$making=$pe->where(array('no'=>$v['people_make_no']))->find();
			$purchaser=$pe->where(array('no'=>$v['people_purchasing_no']))->find();
			$from_company=$c->where(array('no'=>$v['contacts_company_no']))->find();
			$list[$k]['department']=$department['name'];
			$list[$k]['making']=$making['name'];
			$list[$k]['purchaser']=$purchaser['name'];
			$list[$k]['from_company']=$from_company['name'];
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		$this->assign("total_fee",$total_fee);
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	//生产单
    public function product(){
        $m=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$n=M('ProductOrderDetail', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$pe=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$c=M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$s=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$pp=M('Product', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$p=I('p')?I('p'):1;
		$listrows=I('listrows')?I('listrows'):15;
		$firstrow=($p-1)*$listrows;
		$param = I('param.','','trim');
		$purchasing_no=$param['purchasing_no'];
		$map=array();
		if(!empty($purchasing_no)){
			$map['purchasing_no'] = array('like','%'.$purchasing_no.'%');
		}
		$start_time=$param['start_time'];
		$end_time=$param['end_time'];
		if($start_time!='') {
            $map['created_at']= array('gt',$start_time);
        }
        if($end_time!=''){
            $map['created_at'] = array('lt',$end_time);
        }
        if($start_time!='' && $end_time!=''){
            $map['created_at']= array(array('gt',$start_time),array('lt',$end_time));
        }
		// dump($map);
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$total_fee=0;
		foreach($list as $k=>$v){
			$p_list=$n->where(array('plan_id'=>$v['id']))->select();
			$total_price=0;
			$material_total_price=0;
			$scrap_total_price=0;
			foreach($p_list as $ks=>$vs){
				$total_price+=$vs['total_price'];
				
				// $material_total_price=$vs['material_weight']*;
				// $scrap_total_price=$vs['scrap_weight']*;
			}
			$list[$k]['total_price']=$total_price;
			$total_fee+=$total_price;
			$department=$s->where(array('no'=>$v['section_no']))->find();
			$making=$pe->where(array('no'=>$v['people_make_no']))->find();
			$purchaser=$pe->where(array('no'=>$v['people_purchasing_no']))->find();
			$from_company=$c->where(array('no'=>$v['contacts_company_no']))->find();
			$list[$k]['department']=$department['name'];
			$list[$k]['making']=$making['name'];
			$list[$k]['purchaser']=$purchaser['name'];
			$list[$k]['from_company']=$from_company['name'];
		}
		// dump($list);
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		$this->assign("total_fee",$total_fee);
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
}